package com.zzh.takeoutapp.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.MerchantAdapter;
import com.zzh.takeoutapp.adapter.order.more.RvListener;
import com.zzh.takeoutapp.base.BaseFragment;
import com.zzh.takeoutapp.entity.OrderListBean;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.GsonResolve;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/28.
 */

public class MerchantFragment extends BaseFragment {

    @Bind(R.id.vt_address)
    TextView vt_address;//商家地址
    @Bind(R.id.tv_yy_peitime)
    TextView tv_yy_peitime;//配送時間


    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private String shop_id;
    private MerchantAdapter merchantAdapter;
    private final String MERCHANT_FRAGMENT = "MerchantFragment";

    public static MerchantFragment newInstance(String shop_id) {
        MerchantFragment fragment = new MerchantFragment();
        fragment.shop_id = shop_id;
        return fragment;
    }

    @Override
    public void getsavedInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_merchant, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initComponent(View view) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventbusHelper.unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case MERCHANT_FRAGMENT:
                if (jsonObject.optString("message").equals("success")) {
                    JSONObject object = jsonObject.optJSONObject("data");
                    JSONObject detail = object.optJSONObject("detail");
                    vt_address.setText(detail.optString("addr"));

                    JSONArray yy_peitime = detail.optJSONArray("yy_peitime");
                    for (int j = 0; j < yy_peitime.length(); j++) {
                        JSONObject yy_peitime_obj = yy_peitime.optJSONObject(j);
                        String stime = yy_peitime_obj.optString("stime");
                        String ltime = yy_peitime_obj.optString("ltime");
                        tv_yy_peitime.setText("配送时间:" + stime + "-" + ltime);
                    }
                    JSONArray jsonArray = detail.optJSONArray("album");
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        try {
                            String string = jsonArray.getString(i);
                            list.add(string);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    //初始化左侧列表数据
                    merchantAdapter = new MerchantAdapter(getActivity(), list, new RvListener() {
                        @Override
                        public void onItemClick(int id, int position) {

                        }
                    });
                    mRecyclerView.setAdapter(merchantAdapter);
                    merchantAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }
}
